Fix implode losing non-empty-string when ConstantArrayType has all-optional keys#5578
Merged
VincentLanglet merged 3 commits intophpstan:2.1.xfrom May 2, 2026
Merged
Conversation
…tional keys When a ConstantArrayType has all-optional keys (e.g. from union-typed key expressions in loops), ImplodeFunctionReturnTypeExtension's inferConstantType() enumerated every optional-key subset including the empty array. This produced '' as a possible implode result, which generalized to literal-string and lost the non-empty-string guarantee. Pass the isIterableAtLeastOnce() result into inferConstantType() and filter out the empty-array partial when the array is guaranteed non-empty. Closes phpstan/phpstan#14558
Instead of a separate array_filter step before the loop, skip empty partials directly inside the foreach that builds ConstantStringType values. This is simpler and avoids the extra array_filter/array_values calls. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
VincentLanglet
approved these changes
May 1, 2026
staabm
approved these changes
May 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ImplodeFunctionReturnTypeExtension::inferConstantType()to respectisIterableAtLeastOnce()when enumerating optional-key subsets of aConstantArrayType''as a possible result''result generalizes toliteral-stringduring loop iterations, which contaminates the union and causesnon-empty-string/non-falsy-stringto widen to plainstringTest plan
tests/PHPStan/Analyser/nsrt/bug-14558.phpwith both playground scenarios from the issuemake testspasses (11,974 tests, 79,546 assertions)make phpstanpasses (no errors)make cs-fixpasses (no violations)Closes phpstan/phpstan#14558